<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="bank" /><span
              >江苏传智播客教育科技股份有限公司</span
            >
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 主体 -->
        <el-tree default-expand-all :data="data" :props="defaultProps">
          <template slot-scope="{ node, data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="bank" /><span>{{ node.label }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)"
                          >添加子部门</el-dropdown-item
                        >
                        <el-dropdown-item @click.native="hEdit(data.id)"
                          >编辑部门</el-dropdown-item
                        >
                        <el-dropdown-item 
             
                        @click.native="hDel(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>

    <el-dialog 
    v-if="showDialog"
    :close-on-click-modal="false" 
    :close-on-press-escape="false"
    :visible.sync="showDialog"
    :title="isEdit ? '编辑部门' : '添加部门'"
    >
      <DeptDialog
        :pid="curId"
        @success="hSuccess"
        @close="hClose"
        :originList="originList"
        :isEdit="isEdit"
      />
    </el-dialog>
  </div>
</template>
<script>
import { arrayToTree } from "@/utils/index";
import { getDepartment,delDepartment } from "@/api/department";
import DeptDialog from "./deptDialog.vue";
export default {
  components: { DeptDialog },
  created() {
    // 发请求
    this.loadDepartment();
  },
  data() {
    return {
      //收集数据提供给子组件做表单校验时，使用
      originList:[],
      isEdit: false, //是否处于编辑状态
      curId: "",
      showDialog: false,
      // 保存数据
      data: [],
      // 设置具体的替换类名
      defaultProps: {
        children: "children",
        label: "name",
      },
    };
  },
  methods: {
    hClose(){
      this.showDialog = false
    }
    ,
    hEdit(id) {
      this.curId =id;
      this.isEdit = true;
      this.showDialog = true;
    },
    hSuccess() {
      //子组件内部操作成功
      //1.关闭弹层
      this.showDialog = false;
      //2.更新数据
      this.loadDepartment();
    },
    hAdd(id) {
      this.showDialog = true;
      this.curId = id;
      this.isEdit = false
    },
    async doDel(id){
      //调用接口做删除
      await delDepartment(id)
      this.$message.success('删除成功了')
      //更新数据
      this.loadDepartment()
    }
    ,
    hDel(id){
      this.$confirm('你确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
    }).then(()=>{
      this.doDel(id)
    }).catch(()=>{

    })
    }
    ,
    // 加载数据
    async loadDepartment() {
      const res = await getDepartment();
      console.log("res", res);
      // 去掉第一项
      // 第一项就是企业信息，不属于某个部门
      res.data.depts.shift();
        //拿到需要校验数据转递给子组件
      this.originList = res.data.depts.map(item=>{
        return{
          id:item.id,
          pid:item.pid,
          name:item.name,
          code:item.code
        }
      })
      // 数组转成树
      this.data = arrayToTree(res.data.depts);
    },
    handleNodeClick(data) {
      console.log(data);
    },

  },
};
</script>